/*
 **********************************************************
 *
 * Program		:	array.c
 *
 * written by	:	BAYRAMOV Matin
 *
 * description	:	a library with some functions to use arrays
 * 					or array of arrays (matrix).
 *
 * date			:	April 23, 2013
 *
 ***********************************************************
 */


#include "array.h"

/*
 * Function		:	minIndexArray
 *
 * Authors		:	BAYRAMOV Matin
 *
 * Parameters	:	double * array - double array
 * 					int length - size of array
 *
 * Return		:	int minIndex - index of the minimum value
 *
 * Description 	:	Get index of minimum value in the array
 *
 * Complexity	:	O(n) - n is the size of array
 */
int minIndexArray(double * array, int length) {

	if (array == NULL) {
		printf("\nArray is null - minIndexArray\n");
		return ARRAY_NULL;
	}

	if (length <= 0) {
		printf("\nArray lenght is either null or negative - \nminIndexArray\n");
		return ARRAY_LENGTH_NULL;
	}

	int minIndex = 0, var;
	double minValue = array[0];

	for (var = 1; var < length; var++) {
		if (minValue > array[var]) {
			minValue = array[var];
			minIndex = var;
		}
	}

	return minIndex;
}


/*
 * Function		:	printMatrix
 *
 * Authors		:	BAYRAMOV Matin
 *
 * Parameters	:	double ** matrix - double ** array of array (matrix)
 * 					int height - height of matrix
 * 					int lenght - size (length) of matrix
 *
 * Return		:	MATRIX_NULL if matrix is null
 * 					RES_OK if it's ok
 *
 * Description 	:	print a double matrix
 *
 * Complexity	:	O(N*M) - N is height, M is length of matrix
 */
int printMatrix(double ** matrix, int height, int length) {

	if (matrix == NULL) {
		printf("\nNothing to do, matrix is null - printMatrix\n");
		return MATRIX_NULL;
	}

	int var, var2;

	for (var = 0; var < height; var++) {
		printf("\n%d ", var);
		for (var2 = 0; var2 < length; var2++) {
			printf("%f ", matrix[var][var2]);
		}
	}
	printf("\n");

	return RES_OK;
}


/*
 * Function		:	freeMatrix
 *
 * Authors		:	BAYRAMOV Matin
 *
 * Parameters	:	double *** matrix - double ** array of array (matrix)
 * 					int size - height of matrix
 *
 * Return		:	MATRIX_NULL if matrix is null
 * 					RES_OK if it's ok
 *
 * Description 	:	free the memory of a double matrix
 *
 * Complexity	:	O(n) - n is height of matrix
 */
int freeMatrix(double *** matrix, int size) {

	int var;

	if (matrix == NULL) {
		printf("\nNothing to do, already null - freeMatrix\n");
		return MATRIX_NULL;
	}

	if (size <= 0) {
		printf("\nSize is either null or zero - freeMatrix\n");
		return ARRAY_LENGTH_NULL;
	}

	for (var = 0; var < size; var++) {
		if ((*matrix)[var] != NULL) {
			free((*matrix)[var]);
			(*matrix)[var] = NULL;
		}
	}

	free((*matrix));
	(*matrix) = NULL;

	return RES_OK;
}



/*
 * Function		:	createIntArray
 *
 * Authors		:	BAYRAMOV Matin
 *
 * Parameters	:	int ** array - array pointer to create array
 * 					int length - size of array
 *
 * Return		:	ARRAY_NOT_NULL - if the given array is not null
 * 					ARRAY_LENGTH_NULL - if array size if not positive (>0)
 * 					RES_OK - if it's ok.
 *
 * Description 	:	Create an int array with the given size, verify
 * 					size of array, even memory allocation. Set all values
 * 					to zero - uses calloc function.
 *
 * Complexity	:	O(1)
 */
int createIntArray(int **array, int length) {

	// Not used pointer needed
	if ((*array) != NULL) {
		printf("\nArray given is not null ! - createArray\n");
		return ARRAY_NOT_NULL;
	}

	// Length must be positive
	if (length < 1) {
		printf("\nArray size is either null or negative ! - createArray\n");
		return ARRAY_LENGTH_NULL;
	}

	(*array) = (int *) calloc (length, sizeof (int) * length);

	// Verification
	if ((*array) == NULL) {
		printf("\nMemory allocation problem occurred - createArray\n");
		return PROBLEME_MEMOIRE;
	}

	return RES_OK;
}



// Clone an int array with values in it
int cloneIntArray(int * array, int ** clone, int length) {

	if (array == NULL) {
		printf("\nArray given is null - cloneIntArray\n");
		return ARRAY_NULL;
	}

	if ((*clone) != NULL) {
		printf("\nArray clone must be null - cloneIntArray\n");
		return ARRAY_NOT_NULL;
	}

	// Size of array
	if (length < 1) {
		printf("\nArray size is negative - cloneIntArray\n");
		return ARRAY_LENGTH_NULL;
	}

	int res = createIntArray(clone, length);

	if (res != RES_OK) {
		return res;
	}

	// Copy values
	for (res = 0; res < length; res++) {
		(*clone)[res] = array[res];
	}

	return RES_OK;
}

// Print the content of an int array
int printIntArray(int * array, int length) {

	// Verification
	if (array == NULL) {
		printf("\nArray given is null - printIntArray\n");
		return ARRAY_NULL;
	}

	// Size of array
	if (length < 1) {
		printf("\nArray size is negative - printIntArray\n");
		return ARRAY_LENGTH_NULL;
	}

	int var = 0;
	printf("\nArray content : ");
	while (var < length) {
		printf("%d ", array[var]);
		var++;
	}
	printf("\n");

	return RES_OK;
}

